<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      一个问题在于前后都是挨着的，不能连着偷，所以我们要么删掉头部元素，要么删掉尾部元素
      */
      var rob = function (nums) {
        let nums1 = nums.slice(0, nums.length - 1)
        let nums2 = nums.slice(1)

        return Math.max(getRob(nums1), getRob(nums2))

        function getRob(num) {
          let dp = new Array(num.length).fill(0)
          dp[0] = num[0]
          if (num.length <= 1) return num[0]
          dp[1] = Math.max(dp[0], num[1])
          if (nums.length == 2) return dp[1]
          for (let i = 2; i < dp.length; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + num[i])
          }
          return dp[dp.length - 1]
        }
      }
    </script>
  </body>
</html>
